package com.liujianlei.forum_system.controller;

import com.liujianlei.forum_system.common.AppResult;
import com.liujianlei.forum_system.model.Board;
import com.liujianlei.forum_system.service.IBoardService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author tongchen
 * @create 2023-06-08 10:50
 */
@Slf4j
@RequestMapping("/board")
@RestController
@Api(tags = "板块接口")
public class BoardController {
    @Value("${forum.index.board-num:9}")
    private int iBoardNum;
    @Resource
    private IBoardService boardService;
    @PostMapping("/create")
    public AppResult create(@NonNull @RequestParam()@ApiParam("板块名称") String name , @NonNull @RequestParam@ApiParam("板块排序数字") Integer sort){
        //创建对象并填入
        Board board = new Board();
        board.setName(name);
        board.setSort(sort);
        boardService.create(board);
        return AppResult.success("板块插入成功");
    }
    @GetMapping("/topList")
    @ApiOperation("查询top板块信息")
    public AppResult getTopBoard(){
        //直接调用service
        List<Board> boards = boardService.selectTopByNum(iBoardNum);
        return AppResult.success("查询top板块成功",boards);
    }
    @PostMapping("/allNormal")
    @ApiOperation("查询所有正常板块信息")
    public AppResult getAll(){
        //直接调用service
        List<Board> boards = boardService.selectAllNormal();
        return AppResult.success("查询所有正常板块信息成功",boards);
    }
    @ApiOperation("获取版块详情")
    @GetMapping("/getById")
    public AppResult<Board> getBoardInfo (@ApiParam(value = "版块Id") @RequestParam(value = "id") @NonNull Long id) {
        // 调用Service层获取数据
        Board result = boardService.selectById(id);
        return AppResult.success(result);
    }

}
